#ifndef UPDATESERVERSDB_H
#define UPDATESERVERSDB_H

#include "CoreFarmAPI.h"



///////////////////////////////////////////////////////////
//////Update Server NODE AND METHODS///////////////////////
///////////////////////////////////////////////////////////

typedef struct UpdateServerNode{
	char address[SERVER_ADDR_LENGTH];
	int serviceID;
	struct UpdateServerNode *next;

} UpdateServerNode;

UpdateServerNode* updatdeServerNodeConstructor(){
	UpdateServerNode *thisNode;
	thisNode = (UpdateServerNode*)malloc(sizeof(UpdateServerNode));
	assertMem(thisNode);

	(*thisNode).serviceID = 0;
	(*thisNode).next = NULL;
	return thisNode;
}
void updateServerNodeSetAdr(UpdateServerNode *thisNode, char *_adr){ strcpy(thisNode->address, _adr); }
void updateServerNodeSetID(UpdateServerNode *thisNode, int _ID){ thisNode->serviceID = _ID; }
void updateServerNodeSetNext(UpdateServerNode *thisNode, UpdateServerNode *_next){ thisNode->next = _next; }
char* updateServerNodeGetAdr(UpdateServerNode *thisNode){ return thisNode->address; }
int updateServerNodeGetID(UpdateServerNode *thisNode){ return thisNode->serviceID; }
UpdateServerNode* updateServerNodeGetNext(UpdateServerNode *thisNode){ return thisNode->next; }


//////////////////////////////////////////////////////////
// Update Server Database list structure and 'methods ////
//////////////////////////////////////////////////////////


typedef struct UpdateServersDB{
	int size;
	UpdateServerNode *head;

} UpdateServersDB;

UpdateServersDB *updateServers_DB = NULL;

void updateServersDBConstructor(){
	updateServers_DB = (UpdateServersDB*)malloc(sizeof(UpdateServersDB));
	assertMem(updateServers_DB);

	updateServers_DB->size = 0;
	updateServers_DB->head = NULL;
}

void updateServersDBSetHead(UpdateServerNode *head){ updateServers_DB->head = head; }
UpdateServerNode* updateServersDBGetHead(){ return updateServers_DB->head; }

int updateServersDBIsEmpty(){ return (updateServers_DB->size==0); }

void updateServersDBPush(char *addr, int ID){
	UpdateServerNode *newUpdateServer = updatdeServerNodeConstructor();
	updateServerNodeSetID(newUpdateServer, ID);
	updateServerNodeSetAdr(newUpdateServer, addr);

	
	if( !updateServers_DB )
		updateServersDBConstructor();

	if( !updateServersDBIsEmpty() )
		newUpdateServer->next = updateServers_DB->head;

	updateServers_DB ->head = newUpdateServer;
	updateServers_DB->size++;
}

void updateServersPrintAll(){
	UpdateServerNode *currNode = NULL;
	
	if( updateServers_DB)
		currNode = updateServers_DB->head;
	while( currNode ){
		printf("Service ID: %d\nAddress: %s\n", currNode->serviceID, currNode->address);
		currNode = currNode->next;
	}
}

#endif